//
//  HWHomeNormalMoreTypeCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2023/11/16.
//  Copyright © 2023 94Wan. All rights reserved.
//

import UIKit

class HWHomeNormalMoreTypeCell: UITableViewCell {

    var tagCollectionView:TTGTagCollectionView!
    var tagViews:[UIButton] = Array()
    var typeIds:[String] = []
    var model:HWHomeNormalModel?{
        didSet {
            tagViews.removeAll()
            
//            for mod in model?.game_types ?? [] {
//                tagViews.append(newButtonWithTitle(title: mod.name, fontSize: 14*widthScale ,tag: Int(mod.id) ?? 0))
//            }
//            
            for (idx,mod) in (model?.game_types ?? []).enumerated() {
                typeIds.append(mod.id)
                tagViews.append(newButtonWithTitle(title: mod.name, fontSize: 14*widthScale ,tag: idx))
            }
            tagCollectionView.reload()
        }
    }
    var clickTagView:((_ typeId:String,_ clickIndex:Int)->())?
    
    class func cellWithTableView(tableView:UITableView) -> (HWHomeNormalMoreTypeCell) {
        let reUse:String = "HWHomeNormalMoreTypeCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWHomeNormalMoreTypeCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWHomeNormalMoreTypeCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.white
        
        tagCollectionView = TTGTagCollectionView.init(frame: .zero)
        tagCollectionView.delegate = self
        tagCollectionView.dataSource = self
        tagCollectionView.scrollDirection = .vertical
        tagCollectionView.scrollView.isScrollEnabled = false
        tagCollectionView.manualCalculateHeight = true
        tagCollectionView.verticalSpacing = 9*widthScale
        tagCollectionView.horizontalSpacing = 13*widthScale
        tagCollectionView.preferredMaxLayoutWidth = screenWidth-26*widthScale
        contentView.addSubview(tagCollectionView)
    }
    
    func initFrame() -> Void {
        tagCollectionView.snp.makeConstraints { make in
            make.left.equalTo(13*widthScale)
            make.right.equalTo(-13*widthScale)
            make.top.equalTo(10*widthScale)
            make.bottom.equalTo(-10*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension HWHomeNormalMoreTypeCell:TTGTextTagCollectionViewDelegate,TTGTagCollectionViewDelegate, TTGTagCollectionViewDataSource{
    func tagCollectionView(_ tagCollectionView: TTGTagCollectionView!, sizeForTagAt index: UInt) -> CGSize {
        return tagViews[Int(index)].frame.size
    }
    
    func numberOfTags(in tagCollectionView: TTGTagCollectionView!) -> UInt {
        return UInt(tagViews.count)
    }
    
    func tagCollectionView(_ tagCollectionView: TTGTagCollectionView!, tagViewFor index: UInt) -> UIView! {
        if index == self.model?.selectIndex ?? 0 {
            tagViews[Int(index)].setTitleColor(.white, for: .normal)
            tagViews[Int(index)].layer.borderColor = UIColor.white.cgColor
            tagViews[Int(index)].backgroundColor = .colorWithHexString(hexString: "#FA8C08")
        }
        return tagViews[Int(index)]
    }
    
    func tagCollectionView(_ tagCollectionView: TTGTagCollectionView!, didSelectTag tagView: UIView!, at index: UInt) {

    }
    
    func newButtonWithTitle(title:String,fontSize:CGFloat,tag:NSInteger) -> UIButton {
        let button = UIButton(type: .custom)
        button.setTitle(title, for: .normal)
        button.backgroundColor = .white
        button.titleLabel?.font = .systemFont(ofSize: fontSize, weight: .semibold)
        button.setTitleColor(.colorWithHexString(hexString: "#CCCCCC"), for: .normal)
        button.layer.masksToBounds = true
        button.sizeToFit()
        button.layer.borderWidth = 1
        button.layer.borderColor = UIColor.colorWithHexString(hexString: "#CCCCCC").cgColor
        button.layer.cornerRadius = 15*widthScale
        expandSizeForView(view: button, extraWidth: 24*widthScale, extraHeight: 10*widthScale)

        button.tag = tag
        button.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        return button
    }
    
    func expandSizeForView(view:UIView,extraWidth:CGFloat,extraHeight:CGFloat) -> Void {
        var frame = view.frame
        frame.size.height = 30*widthScale
        frame.size.width += extraWidth
        view.frame = frame
    }
    
    @objc func buttonClick(_ button:UIButton) -> Void {
        for btn in tagViews {
            btn.backgroundColor = .white
            btn.layer.borderColor = UIColor.colorWithHexString(hexString: "#CCCCCC").cgColor
            btn.setTitleColor(.colorWithHexString(hexString: "#CCCCCC"), for: .normal)
        }
        button.setTitleColor(.white, for: .normal)
        button.layer.borderColor = UIColor.white.cgColor
        button.backgroundColor = .colorWithHexString(hexString: "#FA8C08")
        self.clickTagView?(typeIds[button.tag],button.tag)
    }
}

